﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package com.fminutes.vinciIsometric.core {
    import flash.events.*;
    import flash.utils.*;
    import flash.display.*;
    import flash.geom.*;
    import com.fminutes.vinciIsometric.events.*;
    import com.fminutes.vinciIsometric.pathfinding.*;
    import __AS3__.vec.*;
    import com.fminutes.vinciIsometric.core.IsoConst.*;

    public class IsoWorld extends Sprite {

        public static const SHOW:int = 2;

        private var _floor:Sprite;
        private var _world:Sprite;
        private var _size:int;
        private var _xNum:int;
        private var _zNum:int;
        private var _map:IsoMap;
        private var isoGrid:IsoGrid;
        private var _objectList:Array;
        private var _objectListChange:Boolean = true;
        private var timer:Timer;
        private var waitingSortList:Vector.<IsoObject>;
        private var refreshSort:Boolean = false;
        private var refreshSortForFloor:Boolean = false;
        private var _sorted:Boolean = true;
        private var _pathfindingMapChanged:Boolean = true;
        private var _pathfindingMap:MapGrid;
        private var _displayRect:Rectangle;
        private var _preOverObject:DisplayObject;
        private var preIsoTryResult:IsoTryResult;
        private var prePoint:Point;

        public function IsoWorld(_arg1:int=100, _arg2:int=100, _arg3:int=16, _arg4:Number=2){
            this._objectList = [];
            this.waitingSortList = new Vector.<IsoObject>();
            this.prePoint = new Point();
            super();
            this.init(_arg1, _arg2, _arg3, _arg4);
            this.timer = new Timer(100);
            this.timer.addEventListener(TimerEvent.TIMER, this.sortHandler);
            this.timer.start();
            addEventListener(PositionEvent.CHANGED, this.positionChangeHandler);
        }
        public function init(_arg1:int=100, _arg2:int=100, _arg3:int=16, _arg4:Number=2):void{
            this.destruction();
            this._size = _arg3;
            this._xNum = _arg1;
            this._zNum = _arg2;
            this._floor = new Sprite();
            addChild(this._floor);
            if (this.isoGrid == null){
                this.isoGrid = new IsoGrid(_arg1, _arg3, _arg4);
            } else {
                this.isoGrid.init(_arg1, _arg3, _arg4);
            };
            this.isoGrid.visible = false;
            this.isoGrid.alpha = 0.2;
            addChild(this.isoGrid);
            this._world = new Sprite();
            addChild(this._world);
            this._map = new IsoMap(_arg1, _arg2);
            dispatchEvent(new IsoWorldEvent(IsoWorldEvent.WORLD_RESIZE));
        }
        public function get isoGridVisible():Boolean{
            if (this.isoGrid != null){
                return (this.isoGrid.visible);
            };
            return (false);
        }
        public function set isoGridVisible(_arg1:Boolean):void{
            if (this.isoGrid != null){
                this.isoGrid.visible = _arg1;
            };
        }
        public function get worldWidth():Number{
            return (((this._xNum + this._zNum) * this.size));
        }
        public function get worldHeight():Number{
            return ((((this._xNum + this._zNum) * this.size) / 2));
        }
        protected function destruction():void{
            var _local1:int;
            if (this._floor != null){
                _local1 = 0;
                while (_local1 < this._floor.numChildren) {
                    (this._floor.getChildAt(_local1) as IsoObject).parentWorld = null;
                    _local1++;
                };
                removeChild(this._floor);
            };
            if (this._world != null){
                _local1 = 0;
                while (_local1 < this._world.numChildren) {
                    (this._world.getChildAt(_local1) as IsoObject).parentWorld = null;
                    _local1++;
                };
                removeChild(this._world);
            };
            if (this.isoGrid != null){
                removeChild(this.isoGrid);
            };
            this._objectList = [];
        }
        public function getObjectList():Array{
            var _local1:Array;
            var _local2:int;
            var _local3:int;
            if (this._objectListChange == false){
                this._objectList;
            } else {
                _local1 = [];
                _local2 = 0;
                _local3 = this._floor.numChildren;
                while (_local2 < _local3) {
                    _local1.push(this._floor.getChildAt(_local2));
                    _local2++;
                };
                _local2 = 0;
                _local3 = this._world.numChildren;
                while (_local2 < _local3) {
                    _local1.push(this._world.getChildAt(_local2));
                    _local2++;
                };
                this._objectListChange = false;
                this._objectList = _local1;
                return (_local1);
            };
            return (this._objectList);
        }
        protected function onAddedToStage(_arg1:Event):void{
        }
        public function addToWorld(_arg1:IsoObject):void{
            if ((_arg1.x + _arg1.floorWidth) > (this._xNum * this.size)){
                _arg1.x = ((this._xNum * this.size) - _arg1.floorWidth);
            };
            if ((_arg1.z + _arg1.floorHeight) > (this._zNum * this.size)){
                _arg1.z = ((this._zNum * this.size) - _arg1.floorHeight);
            };
            if (_arg1.type == IsoType.STATIONARY){
                _arg1.parentWorld = this;
                this._world.addChild(_arg1);
                this._map.addIso(_arg1);
            } else {
                if (_arg1.type == IsoType.FLOOR){
                    _arg1.parentWorld = this;
                    this._floor.addChild(_arg1);
                    this._map.addIso(_arg1);
                } else {
                    if (_arg1.type == IsoType.MOVING){
                        _arg1.parentWorld = this;
                        this._world.addChild(_arg1);
                    };
                };
            };
            this.comboHandler(_arg1, true);
            this._pathfindingMapChanged = true;
            this.addToWaitingSortList(_arg1);
            this._objectListChange = true;
        }
        public function removeFromWorld(_arg1:IsoObject):void{
            if (_arg1.type == IsoType.STATIONARY){
                this._world.removeChild(_arg1);
                this._map.removeIso(_arg1);
                _arg1.parentWorld = null;
            } else {
                if (_arg1.type == IsoType.FLOOR){
                    this._floor.removeChild(_arg1);
                    this._map.removeIso(_arg1);
                    _arg1.parentWorld = null;
                } else {
                    if (_arg1.type == IsoType.MOVING){
                        this._world.removeChild(_arg1);
                        _arg1.parentWorld = null;
                    };
                };
            };
            this.comboHandler(_arg1, false);
            this._objectListChange = true;
            this._pathfindingMapChanged = true;
        }
        private function comboHandler(_arg1:IsoObject, _arg2:Boolean=true):void{
            var _local3:Array;
            var _local4:String;
            if (_arg1.isCombo){
                _local3 = this.getIsoObjectAround(_arg1);
                _local4 = this.getDisplayChildString(_local3, _arg2);
                _arg1.comboRim = _local4;
                _arg1.refresh();
            };
        }
        private function addToWaitingSortList(_arg1:IsoObject):void{
            if (_arg1.type == IsoType.MOVING){
                this.waitingSortList.push(_arg1);
            } else {
                if (_arg1.type == IsoType.FLOOR){
                    this.refreshSortForFloor = true;
                } else {
                    if (_arg1.type == IsoType.STATIONARY){
                        this.refreshSort = true;
                    };
                };
            };
        }
        private function positionChangeHandler(_arg1:PositionEvent):void{
            if ((_arg1.target is IsoObject)){
                this.addToWaitingSortList((_arg1.target as IsoObject));
            };
        }
        private function sortHandler(_arg1:Event):void{
            if (this.sorted){
                if (this.refreshSort == true){
                    this.sort();
                    this.refreshSort = false;
                    this.waitingSortList.length = 0;
                } else {
                    if (this.waitingSortList.length > 0){
                        while (this.waitingSortList.length > 0) {
                            this.insertObject(this.waitingSortList.shift());
                        };
                    };
                };
                if (this.refreshSortForFloor == true){
                    this.sortFloor();
                    this.refreshSortForFloor = false;
                };
            };
        }
        public function get sorted():Boolean{
            return (this._sorted);
        }
        public function set sorted(_arg1:Boolean):void{
            this._sorted = _arg1;
        }
        private function insertObject(_arg1:IsoObject):void{
            var _local2:int;
            var _local5:IsoObject;
            if (this._world.contains(_arg1) == false){
                return;
            };
            var _local3:int;
            var _local4:Boolean;
            _local2 = 0;
            while (_local2 < this._world.numChildren) {
                _local5 = (this._world.getChildAt(_local2) as IsoObject);
                if (((!((_arg1 == _local5))) && ((this.simpleSortFn(_arg1, _local5) == -1)))){
                    if (this._world.getChildIndex(_arg1) < _local2){
                        _local3 = (_local2 - 1);
                    } else {
                        _local3 = _local2;
                    };
                    _local4 = true;
                    break;
                };
                _local2++;
            };
            if (_local4 == false){
                _local3 = (this._world.numChildren - 1);
            };
            this._world.setChildIndex(_arg1, _local3);
        }
        private function getDisplayChildString(_arg1:Array, _arg2:Boolean=true):String{
            var _local5:IsoObject;
            var _local6:int;
            var _local7:String;
            var _local8:Array;
            var _local3 = "";
            var _local4:int;
            while (_local4 < _arg1.length) {
                if (_arg1[_local4] == null){
                    _local3 = (_local3 + "1");
                } else {
                    _local3 = (_local3 + "0");
                    _local5 = (_arg1[_local4] as IsoObject);
                    if (_local4 == 0){
                        _local6 = 2;
                    } else {
                        if (_local4 == 1){
                            _local6 = 3;
                        } else {
                            if (_local4 == 2){
                                _local6 = 0;
                            } else {
                                if (_local4 == 3){
                                    _local6 = 1;
                                };
                            };
                        };
                    };
                    _local7 = _local5.comboRim;
                    _local8 = _local7.split("");
                    if (_arg2){
                        _local8[_local6] = 0;
                    } else {
                        _local8[_local6] = 1;
                    };
                    _local7 = _local8.join("");
                    _local5.comboRim = _local7;
                    _local5.refresh();
                };
                _local4++;
            };
            if (_arg2 == false){
                _local3 = "1111";
            };
            return (_local3);
        }
        public function moveIso(_arg1:IsoObject, _arg2:Vector3D, _arg3:int):void{
            this._map.removeIso(_arg1);
            _arg1.xyz = _arg2;
            _arg1.direction = _arg3;
            this._map.addIso(_arg1);
        }
        public function get pathfindingMap():MapGrid{
            var _local1:MapGrid;
            var _local2:int;
            var _local3:int;
            var _local4:int;
            if (this._pathfindingMapChanged){
                _local1 = new MapGrid(this._xNum, this._zNum);
                _local2 = 0;
                while (_local2 < this._xNum) {
                    _local3 = 0;
                    while (_local3 < this._zNum) {
                        _local4 = this._map.getMapItem(_local2, _local3).walkable;
                        _local1.setWalkable(_local2, _local3, _local4);
                        _local3++;
                    };
                    _local2++;
                };
                this._pathfindingMap = _local1;
                this._pathfindingMapChanged = false;
            };
            return (this._pathfindingMap);
        }
        public function sortFloor():void{
            var _local2:int;
            var _local1:Array = [];
            _local2 = 0;
            while (_local2 < this._floor.numChildren) {
                _local1.push(this._floor.getChildAt(_local2));
                _local2++;
            };
            _local1.sort(this.sortFloorFn);
            _local2 = 0;
            while (_local2 < _local1.length) {
                this._floor.setChildIndex(_local1[_local2], _local2);
                _local2++;
            };
        }
        private function sortFloorFn(_arg1:IsoObject, _arg2:IsoObject):Number{
            if (_arg1.layerIndex < _arg2.layerIndex){
                return (-1);
            };
            if (_arg1.layerIndex > _arg2.layerIndex){
                return (1);
            };
            if (_arg1.y2d < _arg2.y2d){
                return (-1);
            };
            return (1);
        }
        public function sort():void{
            var _local4:IsoObject;
            var _local1:Number = getTimer();
            var _local2:Vector.<IsoObject> = new Vector.<IsoObject>();
            var _local3:int;
            _local3 = 0;
            while (_local3 < this._world.numChildren) {
                _local4 = (this._world.getChildAt(_local3) as IsoObject);
                _local2.push(_local4);
                _local3++;
            };
            _local2.sort(this.sortFn);
            _local3 = 0;
            while (_local3 < _local2.length) {
                this._world.setChildIndex(_local2[_local3], _local3);
                _local3++;
            };
        }
        private function simpleSortFn(_arg1:IsoObject, _arg2:IsoObject):Number{
            var _local3:IsoPosition = _arg1.isoPosition;
            var _local4:IsoPosition = _arg2.isoPosition;
            var _local5:IsoPosition = _arg1.isoMaxPosition;
            var _local6:IsoPosition = _arg2.isoMaxPosition;
            if ((((_local5.x >= _local4.x)) && ((_local5.z >= _local4.z)))){
                return (1);
            };
            if ((((_local6.x >= _local3.x)) && ((_local6.z >= _local3.z)))){
                return (-1);
            };
            return (1);
        }
        private function sortFn(_arg1:IsoObject, _arg2:IsoObject):Number{
            var _local10:int;
            var _local11:int;
            var _local12:IsoMapItem;
            var _local13:IsoObject;
            var _local14:Number;
            var _local15:Number;
            var _local3:IsoPosition = _arg1.isoPosition;
            var _local4:IsoPosition = _arg2.isoPosition;
            var _local5:IsoPosition = _arg1.isoMaxPosition;
            var _local6:IsoPosition = _arg2.isoMaxPosition;
            var _local7:int;
            var _local8:int = _arg1.isoID;
            var _local9:int = _arg2.isoID;
            if ((((((((((_local3.x <= _local6.x)) && ((_local3.z <= _local6.z)))) && ((_local5.x >= _local4.x)))) && ((_local5.z >= _local4.z)))) || ((((((((_local3.x >= _local6.x)) && ((_local3.z >= _local6.z)))) && ((_local5.x <= _local4.x)))) && ((_local5.z <= _local4.z)))))){
                if (_arg1.wallpaper == true){
                    return (-1);
                };
                if (_arg2.wallpaper == true){
                    return (1);
                };
                if (_arg1.y > _arg2.y){
                    return (-1);
                };
                if (_arg1.y < _arg2.y){
                    return (1);
                };
                _local14 = ((_arg1.x + _arg1.z) + ((_arg1.floorWidth + _arg1.floorHeight) / 2));
                _local15 = ((_arg2.x + _arg2.z) + ((_arg2.floorWidth + _arg2.floorHeight) / 2));
                if (_local14 < _local15){
                    return (-1);
                };
                if (_local14 > _local15){
                    return (1);
                };
                if (_arg1.type == IsoType.STATIONARY){
                    return (1);
                };
                return (-1);
            };
            if ((((_local5.x >= _local4.x)) && ((_local5.z >= _local4.z)))){
                return (1);
            };
            if ((((_local6.x >= _local3.x)) && ((_local6.z >= _local3.z)))){
                return (-1);
            };
            if (_local3.x < _local4.x){
                _local10 = _local5.x;
                _local11 = _local3.z;
                while ((((_local11 >= _local4.z)) && ((_local11 > 0)))) {
                    _local11--;
                    _local12 = this._map.getMapItem(_local10, _local11);
                    if (_local12 == null){
                    } else {
                        _local13 = this._map.getMapItem(_local10, _local11).baseIsoObject;
                        if (_local13 == null){
                        } else {
                            if ((((_local13.isoMaxPosition.x >= _local4.x)) && ((_local13.isoMaxPosition.z >= _local4.z)))){
                                return (1);
                            };
                            _local10 = _local13.isoMaxPosition.x;
                        };
                    };
                };
                _local10 = _local5.x;
                _local11 = _local3.z;
                while ((((_local10 <= _local4.x)) && ((_local10 < (this._xNum - 2))))) {
                    _local10 = (_local10 + 1);
                    _local12 = this._map.getMapItem(_local10, _local11);
                    if (_local12 == null){
                    } else {
                        _local13 = this._map.getMapItem(_local10, _local11).baseIsoObject;
                        if (_local13 == null){
                        } else {
                            if ((((_local6.x >= _local13.isoPosition.x)) && ((_local6.z >= _local13.isoPosition.z)))){
                                return (-1);
                            };
                            _local11 = _local13.isoPosition.z;
                        };
                    };
                };
            } else {
                if (_local3.z < _local4.z){
                    _local10 = _local3.x;
                    _local11 = _local5.z;
                    while ((((_local10 >= _local4.x)) && ((_local10 > 0)))) {
                        _local10--;
                        _local12 = this._map.getMapItem(_local10, _local11);
                        if (_local12 == null){
                        } else {
                            _local13 = this._map.getMapItem(_local10, _local11).baseIsoObject;
                            if (_local13 == null){
                            } else {
                                if ((((_local13.isoMaxPosition.x >= _local4.x)) && ((_local13.isoMaxPosition.z >= _local4.z)))){
                                    return (1);
                                };
                                _local11 = _local13.isoMaxPosition.z;
                            };
                        };
                    };
                    _local10 = _local3.x;
                    _local11 = _local5.z;
                    while ((((_local11 <= _local4.z)) && ((_local11 < (this._zNum - 2))))) {
                        _local11 = (_local11 + 1);
                        _local12 = this._map.getMapItem(_local10, _local11);
                        if (_local12 == null){
                        } else {
                            _local13 = this._map.getMapItem(_local10, _local11).baseIsoObject;
                            if (_local13 == null){
                            } else {
                                if ((((_local6.x >= _local13.isoPosition.x)) && ((_local6.z >= _local13.isoPosition.z)))){
                                    return (-1);
                                };
                                _local10 = _local13.isoPosition.x;
                            };
                        };
                    };
                };
            };
            return (1);
        }
        public function get size():int{
            return (this._size);
        }
        public function get xNum():int{
            return (this._xNum);
        }
        public function get zNum():int{
            return (this._zNum);
        }
        public function set displayRect(_arg1:Rectangle):void{
            this._displayRect = _arg1;
        }
        public function get displayRect():Rectangle{
            if (this._displayRect == null){
                return (new Rectangle(0, 0, this.width, this.height));
            };
            return (this._displayRect);
        }
        public function getMousePosition(_arg1:Number=0, _arg2:Number=0):IsoPosition{
            var _local3:Vector3D = IsoUtils.screenToIso(new Point((mouseX + _arg1), (mouseY + _arg2)));
            var _local4:IsoPosition = new IsoPosition(Math.floor((_local3.x / this.size)), Math.floor((_local3.z / this.size)));
            return (_local4);
        }
        public function getMousePoint(_arg1:Number=0, _arg2:Number=0, _arg3:Number=0):Point{
            var _local4:Vector3D = IsoUtils.screenToIso(new Point((mouseX + _arg2), (mouseY + _arg3)));
            _local4.x = (Math.floor((_local4.x / this.size)) * this.size);
            _local4.y = _arg1;
            _local4.z = (Math.floor((_local4.z / this.size)) * this.size);
            return (IsoUtils.isoToScreen(_local4));
        }
        public function getObjectUnderXY(_arg1:int, _arg2:int):Array{
            var _local4:int;
            var _local5:int;
            var _local6:DisplayObject;
            var _local3:Array = [];
            _local4 = 0;
            _local5 = this._floor.numChildren;
            while (_local4 < _local5) {
                _local6 = (this._floor.getChildAt(_local4) as IsoObject).hitTest(_arg1, _arg2);
                if (_local6 != null){
                    _local3.push(_local6);
                };
                _local4++;
            };
            _local4 = 0;
            _local5 = this._world.numChildren;
            while (_local4 < _local5) {
                _local6 = (this._world.getChildAt(_local4) as IsoObject).hitTest(_arg1, _arg2);
                if (_local6 != null){
                    _local3.push(_local6);
                };
                _local4++;
            };
            return (_local3);
        }
        public function getPointWall(_arg1:Number, _arg2:Number):IsoObject{
            var _local3:Point = new Point(_arg1, _arg2);
            _local3 = localToGlobal(_local3);
            var _local4:Array = this.getObjectUnderXY(_local3.x, _local3.y);
            var _local5:int;
            while (_local5 < _local4.length) {
                if ((((_local4[_local5] is IsoObject)) && ((_local4[_local5] as IsoObject).isWall))){
                    return ((_local4[_local5] as IsoObject));
                };
                _local5++;
            };
            return (null);
        }
        public function getMouseWall(_arg1:Number=0, _arg2:Number=0):IsoObject{
            return (this.getPointWall((mouseX + _arg1), (mouseY + _arg2)));
        }
        public function executeClick(_arg1:MouseEvent):void{
            var _local5:IsoObjectMouseEvent;
            if (stage == null){
                return;
            };
            var _local2:Number = stage.mouseX;
            var _local3:Number = stage.mouseY;
            var _local4:DisplayObject = this.hitDisplayObject(_local2, _local3);
            if (_local4 != null){
                _local5 = new IsoObjectMouseEvent(IsoObjectMouseEvent.MOUSE_CLICK, true);
                _local5.eventObject = _local4;
                _local5.isoPosition = this.getRightIsoPosition(this.getMousePosition());
                _local4.dispatchEvent(_local5);
            } else {
                this.dispatchEvent(new IsoObjectMouseEvent(IsoObjectMouseEvent.MOUSE_CLICK_SPACE, true));
            };
        }
        private function getRightIsoPosition(_arg1:IsoPosition):IsoPosition{
            if (_arg1.x < 0){
                _arg1.x = 0;
            };
            if (_arg1.z < 0){
                _arg1.z = 0;
            };
            if (_arg1.x >= this._xNum){
                _arg1.x = (this._xNum - 1);
            };
            if (_arg1.z >= this._zNum){
                _arg1.z = (this._zNum - 1);
            };
            return (_arg1);
        }
        public function executeMouseOver(_arg1:MouseEvent):void{
            var _local5:IsoObjectMouseEvent;
            var _local6:IsoObjectMouseEvent;
            var _local7:IsoObjectMouseEvent;
            if (stage == null){
                return;
            };
            var _local2:Number = stage.mouseX;
            var _local3:Number = stage.mouseY;
            var _local4:DisplayObject = this.hitDisplayObject(_local2, _local3, true);
            if (_local4){
                _local5 = new IsoObjectMouseEvent(IsoObjectMouseEvent.MOUSE_MOVE, true);
                _local5.eventObject = _local4;
                _local5.isoPosition = this.getRightIsoPosition(this.getMousePosition());
                _local4.dispatchEvent(_local5);
            };
            if (_local4 == this._preOverObject){
                return;
            };
            if (((!((_local4 == this._preOverObject))) && (!((this._preOverObject == null))))){
                _local6 = new IsoObjectMouseEvent(IsoObjectMouseEvent.MOUSE_OUT, true);
                _local6.eventObject = this._preOverObject;
                _local6.isoPosition = this.getRightIsoPosition(this.getMousePosition());
                this._preOverObject.dispatchEvent(_local6);
                this._preOverObject = null;
            };
            if (_local4 != null){
                _local7 = new IsoObjectMouseEvent(IsoObjectMouseEvent.MOUSE_OVER, true);
                _local7.eventObject = _local4;
                _local7.isoPosition = this.getRightIsoPosition(this.getMousePosition());
                this._preOverObject = _local4;
                _local4.dispatchEvent(_local7);
            };
        }
        private function hitDisplayObject(_arg1:Number, _arg2:Number, _arg3:Boolean=false):DisplayObject{
            var _local4:int;
            var _local5:int;
            var _local6:DisplayObject;
            var _local7:IsoObject;
            _local4 = (this._world.numChildren - 1);
            _local5 = 0;
            while (_local4 >= _local5) {
                _local7 = (this._world.getChildAt(_local4) as IsoObject);
                if ((((((_arg3 == false)) || ((_local7.mouseOverEnabled == true)))) && ((_local7.visible == true)))){
                    _local6 = _local7.hitTest(_arg1, _arg2);
                    if (_local6 != null){
                        return (_local6);
                    };
                };
                _local4--;
            };
            _local4 = (this._floor.numChildren - 1);
            _local5 = 0;
            while (_local4 >= _local5) {
                _local7 = (this._floor.getChildAt(_local4) as IsoObject);
                if ((((((_arg3 == false)) || ((_local7.mouseOverEnabled == true)))) && ((_local7.visible == true)))){
                    _local6 = _local7.hitTest(_arg1, _arg2);
                    if (_local6 != null){
                        return (_local6);
                    };
                };
                _local4--;
            };
            return (null);
        }
        public function getIsoMapItem(_arg1:IsoPosition):IsoMapItem{
            var _local2:int;
            var _local3:int;
            if ((((((((_arg1.x >= 0)) && ((_arg1.x < this._xNum)))) && ((_arg1.z >= 0)))) && ((_arg1.z < this._zNum)))){
                _local2 = _arg1.x;
                _local3 = _arg1.z;
                return (this._map.getMapItem(_local2, _local3));
            };
            return (null);
        }
        public function getRectIsoObject(_arg1:IsoPosition, _arg2:IsoPosition):Array{
            var _local6:int;
            var _local7:int;
            var _local3:Array = [];
            var _local4:int = _arg1.x;
            var _local5:int = _arg2.x;
            while (_local4 <= _local5) {
                _local6 = _arg1.z;
                _local7 = _arg2.z;
                while (_local6 <= _local7) {
                    _local3.push(this.getIsoMapItem(new IsoPosition(_local4, _local6)));
                    _local6++;
                };
                _local4++;
            };
            return (_local3);
        }
        private function getObjectForIsoMapItem(_arg1:IsoPosition):IsoObject{
            var _local2:IsoMapItem = this.getIsoMapItem(_arg1);
            var _local3:Array = _local2.isoList;
            var _local4:int;
            while (_local4 < _local3.length) {
                if ((_local3[_local4] as IsoObject).isWall){
                    return (_local3[_local4]);
                };
                _local4++;
            };
            return (null);
        }
        private function getVirtualWall(_arg1:IsoObject):VirtualWall{
            var _local2:IsoPosition;
            var _local4:int;
            var _local5:IsoObject;
            var _local3:Vector.<IsoObject> = new Vector.<IsoObject>();
            if (_arg1.direction == 0){
                _local2 = _arg1.isoPosition;
                _local4 = _local2.x;
                while (_local4 < this.xNum) {
                    _local5 = this.getObjectForIsoMapItem(new IsoPosition(_local4, _local2.z));
                    if (_local5 == null){
                        break;
                    };
                    _local3.push(_local5);
                    _local4++;
                };
                _local4 = _local2.x;
                while (_local4 >= 0) {
                    _local5 = this.getObjectForIsoMapItem(new IsoPosition(_local4, _local2.z));
                    if (_local5 == null){
                        break;
                    };
                    _local3.push(_local5);
                    _local4--;
                };
            } else {
                _local2 = _arg1.isoPosition;
                _local4 = _local2.z;
                while (_local4 < this.zNum) {
                    _local5 = this.getObjectForIsoMapItem(new IsoPosition(_local2.x, _local4));
                    if (_local5 == null){
                        break;
                    };
                    _local3.push(_local5);
                    _local4++;
                };
                _local4 = _local2.z;
                while (_local4 >= 0) {
                    _local5 = this.getObjectForIsoMapItem(new IsoPosition(_local2.x, _local4));
                    if (_local5 == null){
                        break;
                    };
                    _local3.push(_local5);
                    _local4--;
                };
            };
            var _local6:int = _arg1.x;
            var _local7:int = _arg1.z;
            var _local8:int = _arg1.x;
            var _local9:int = _arg1.z;
            var _local10:int = _arg1.y;
            _local4 = 0;
            while (_local4 < _local3.length) {
                if (_arg1.direction == 0){
                    if (_local3[_local4].x < _local6){
                        _local6 = _local3[_local4].x;
                    };
                    if (_local3[_local4].x > _local8){
                        _local8 = _local3[_local4].x;
                    };
                } else {
                    if (_local3[_local4].z < _local7){
                        _local7 = _local3[_local4].z;
                    };
                    if (_local3[_local4].z > _local9){
                        _local9 = _local3[_local4].z;
                    };
                };
                _local4++;
            };
            var _local11:VirtualWall = new VirtualWall();
            _local11.xyz = new Vector3D(_local6, _local10, _local7);
            _local11.floorWidth = ((_local8 - _local6) + _arg1.floorWidth);
            _local11.floorHeight = ((_local9 - _local7) + _arg1.floorHeight);
            _local11.direction = _arg1.direction;
            _local11.x = _local11.xyz.x;
            _local11.y = _local11.xyz.y;
            _local11.z = _local11.xyz.z;
            _local11.altitude = _arg1.altitude;
            return (_local11);
        }
        public function tryHandle(_arg1:IsoObject, _arg2:Number, _arg3:Number):IsoTryResult{
            var _local4:IsoTryResult;
            var _local5:Number;
            var _local6:Number;
            var _local7:IsoObject;
            var _local8:VirtualWall;
            var _local9:IsoTryResult;
            var _local10:IsoPosition;
            if (_arg1 == null){
                _local4 = new IsoTryResult();
            } else {
                if (_arg1.canAttached){
                    _local5 = _arg2;
                    _local6 = _arg3;
                    _local7 = this.getPointWall(_local5, _local6);
                    if (_local7 != null){
                        _local8 = this.getVirtualWall(_local7);
                        _local4 = this.tryHandle_attached(_arg1, _local8, _arg2, _arg3);
                    } else {
                        _local9 = new IsoTryResult();
                        _local10 = _arg1.getIsoPosition2D(_arg2, _arg3);
                        _local9.x = (_local10.x * this.size);
                        _local9.z = (_local10.z * this.size);
                        _local4 = _local9;
                    };
                } else {
                    if (_arg1.afloat){
                        _local4 = this.tryHandler_afloat(_arg1, _arg2, _arg3);
                    } else {
                        if ((((_arg1.type == IsoType.FLOOR)) && (_arg1.isContainer))){
                            _local4 = this.tryHandler_floor(_arg1, _arg2, _arg3);
                        } else {
                            _local4 = this.tryHandle_put(_arg1, _arg2, _arg3);
                        };
                    };
                };
            };
            return (_local4);
        }
        private function tryHandler_floor(_arg1:IsoObject, _arg2:Number, _arg3:Number):IsoTryResult{
            var _local4:IsoTryResult = new IsoTryResult();
            var _local5:IsoPosition = _arg1.getIsoPosition2D(_arg2, _arg3);
            var _local6:IsoPosition = _arg1.getIsoMaxPosition2D(_arg2, _arg3);
            var _local7:Array = this.getRectIsoObject(_local5, _local6);
            _local4.success = true;
            var _local8:int;
            while (_local8 < _local7.length) {
                if (_local7[_local8] == null){
                    _local4.success = false;
                };
                _local8++;
            };
            _local4.y = 0;
            _local4.x = (_local5.x * this.size);
            _local4.z = (_local5.z * this.size);
            _local4.position = new IsoPosition(_local5.x, _local5.z);
            return (_local4);
        }
        private function tryHandler_afloat(_arg1:IsoObject, _arg2:Number, _arg3:Number):IsoTryResult{
            var _local4:IsoTryResult = new IsoTryResult();
            var _local5:IsoObject;
            var _local6:Boolean;
            var _local7:Number = 0;
            var _local8:IsoPosition = _arg1.getIsoPosition2D(_arg2, _arg3);
            var _local9:IsoPosition = _arg1.getIsoMaxPosition2D(_arg2, _arg3);
            var _local10:Object = this.getRectTopIsoObject(_local8, _local9);
            _local5 = (_local10.topIsoObject as IsoObject);
            var _local11:Boolean = _local10.success;
            _local7 = _local10.maxY;
            if (((_local11) && ((_local5 == null)))){
                _local4.success = true;
                _local4.y = _arg1.y;
            } else {
                if (((_local11) && (_local5.admissible(_arg1)))){
                    _local4.success = true;
                    _local4.y = _arg1.y;
                } else {
                    _local4.success = false;
                    _local4.y = _arg1.y;
                };
            };
            _local4.x = (_local8.x * this.size);
            _local4.z = (_local8.z * this.size);
            _local4.position = new IsoPosition(_local8.x, _local8.z);
            return (_local4);
        }
        private function tryHandle_attached(_arg1:IsoObject, _arg2:VirtualWall, _arg3:Number, _arg4:Number):IsoTryResult{
            var _local6:Boolean;
            var _local17:Array;
            var _local18:int;
            var _local19:IsoObject;
            var _local5:IsoTryResult = new IsoTryResult();
            var _local7:Vector3D = _arg2.xyz.clone();
            if (_arg1.direction != _arg2.direction){
                _arg1.direction = _arg2.direction;
                _arg1.refresh();
            };
            if (_arg2.direction == 0){
                _local6 = true;
                _local7.z = (_local7.z + _arg2.floorHeight);
            } else {
                _local6 = false;
                _local7.x = (_local7.x + _arg2.floorWidth);
            };
            var _local8:Number = _arg3;
            var _local9:Number = _arg4;
            var _local10:Vector3D = IsoUtils.screenToIsoB(new Point(_local8, _local9), _local7, _local6);
            _local5.success = true;
            _local5.position = new IsoPosition((_local10.x / this.size), (_local10.z / this.size));
            _local5.x = (int((_local10.x / this.size)) * this.size);
            _local5.y = _local10.y;
            _local5.z = (int((_local10.z / this.size)) * this.size);
            if (_arg2.direction == 0){
                if ((((_local5.x < _arg2.x)) || (((_local5.x + _arg1.floorWidth) > (_arg2.x + _arg2.floorWidth))))){
                    _local5.success = false;
                };
            } else {
                if ((((_local5.z < _arg2.z)) || (((_local5.z + _arg1.floorHeight) > (_arg2.z + _arg2.floorHeight))))){
                    _local5.success = false;
                };
            };
            _local5.y = (int((_local5.y / 10)) * 10);
            if ((_local5.y - _arg1.altitude) <= -(_arg2.altitude)){
                _local5.success = false;
            };
            var _local11:IsoPosition = _local5.position;
            var _local12:int = ((_arg1.floorWidth >= 1)) ? _arg1.floorWidth : 1;
            var _local13:int = ((_arg1.floorHeight >= 1)) ? _arg1.floorHeight : 1;
            var _local14:IsoPosition = new IsoPosition((_local11.x + ((_local12 - 1) / this.size)), (_local11.z + ((_local13 - 1) / this.size)));
            var _local15:Array = this.getRectIsoObject(_local11, _local14);
            var _local16:int;
            while (_local16 < _local15.length) {
                if ((_local15[_local16] as IsoMapItem) == null){
                    _local17 = [];
                } else {
                    _local17 = (_local15[_local16] as IsoMapItem).stationaryList;
                };
                if (_local17 == null){
                    _local17 = [];
                };
                _local18 = 0;
                while (_local18 < _local17.length) {
                    _local19 = _local17[_local18];
                    if (((((_local5.y - _arg1.altitude) < _local19.y)) && ((_local5.y > (_local19.y - _local19.altitude))))){
                        if (((!((_local19.wallpaper == true))) && (!((_arg1.wallpaper == true))))){
                            _local5.success = false;
                            break;
                        };
                    };
                    _local18++;
                };
                _local16++;
            };
            return (_local5);
        }
        private function tryHandle_put(_arg1:IsoObject, _arg2:Number, _arg3:Number):IsoTryResult{
            var _local4:IsoTryResult = new IsoTryResult();
            var _local5:IsoObject;
            var _local6:Boolean;
            var _local7:Number = 0;
            var _local8:IsoPosition = _arg1.getIsoPosition2D(_arg2, _arg3);
            var _local9:IsoPosition = _arg1.getIsoMaxPosition2D(_arg2, _arg3);
            var _local10:Object = this.getRectTopIsoObject(_local8, _local9, false);
            _local5 = (_local10.topIsoObject as IsoObject);
            var _local11:Boolean = _local10.success;
            _local7 = _local10.maxY;
            if (((_local11) && ((_local5 == null)))){
                _local4.success = true;
                _local4.y = 0;
            } else {
                if (((_local11) && (_local5.admissible(_arg1)))){
                    _local4.success = true;
                    _local4.y = _local7;
                } else {
                    _local4.success = false;
                    _local4.y = _local7;
                };
            };
            _local4.x = (_local8.x * this.size);
            _local4.z = (_local8.z * this.size);
            _local4.position = new IsoPosition(_local8.x, _local8.z);
            return (_local4);
        }
        private function getRectTopIsoObject(_arg1:IsoPosition, _arg2:IsoPosition, _arg3:Boolean=true):Object{
            var _local9:IsoObject;
            var _local4:Array = this.getRectIsoObject(_arg1, _arg2);
            var _local5:IsoObject;
            var _local6:Number = 0;
            var _local7:int;
            var _local8:int = _local4.length;
            while (_local7 < _local8) {
                if (_local4[_local7] == null){
                    return ({
                        maxY:0,
                        topIsoObject:null,
                        success:false
                    });
                };
                _local9 = (_local4[_local7] as IsoMapItem).getTopIsoObject(_arg3);
                if (((!((_local9 == null))) && (((_local9.y - _local9.altitude) < _local6)))){
                    _local6 = (_local9.y - _local9.altitude);
                };
                if (((!((_local7 == 0))) && (!((_local9 == _local5))))){
                    return ({
                        maxY:_local6,
                        topIsoObject:null,
                        success:false
                    });
                };
                _local5 = _local9;
                _local7++;
            };
            return ({
                maxY:_local6,
                topIsoObject:_local5,
                success:true
            });
        }
        public function getComboList(_arg1:IsoObject):Array{
            var _local4:IsoObject;
            var _local5:Array;
            var _local6:int;
            var _local2:Array = [];
            var _local3:Array = [];
            _local3.push(_arg1);
            while (_local3.length > 0) {
                _local4 = _local3.shift();
                _local5 = this.getIsoObjectAround(_local4);
                _local6 = 0;
                while (_local6 < _local5.length) {
                    if (((((!((_local5[_local6] == null))) && ((_local2.indexOf(_local5[_local6]) == -1)))) && ((_local3.indexOf(_local5[_local6]) == -1)))){
                        _local3.push(_local5[_local6]);
                    };
                    _local6++;
                };
                _local2.push(_local4);
            };
            return (_local2);
        }
        public function getIsoObjectAround(_arg1:IsoObject, _arg2:Vector3D=null):Array{
            var _local4:IsoPosition;
            var _local5:IsoPosition;
            var _local6:int;
            var _local7:int;
            var _local8:int;
            var _local9:int;
            var _local10:IsoObject;
            var _local11:int;
            var _local12:int;
            var _local13:int;
            var _local14:int;
            var _local3:Array = [null, null, null, null];
            if (_arg1.isCombo){
                if (_arg2 == null){
                    _local4 = _arg1.isoPosition;
                    _local5 = _arg1.isoMaxPosition;
                } else {
                    _local11 = int((_arg2.x / this._size));
                    _local12 = int((_arg2.z / this._size));
                    _local13 = (((_arg2.x + _arg1.floorWidth) - 1) / this._size);
                    _local14 = (((_arg2.z + _arg1.floorHeight) - 1) / this._size);
                    _local4 = new IsoPosition(_local11, _local12);
                    _local5 = new IsoPosition(_local13, _local14);
                };
                _local6 = _local4.x;
                _local7 = _local5.x;
                _local8 = _local4.z;
                _local9 = _local5.z;
                _local10 = this.getComboRim(_local6, _local7, (_local8 - 1), (_local8 - 1), _arg1.isoName);
                _local3[0] = _local10;
                _local10 = this.getComboRim((_local7 + 1), (_local7 + 1), _local8, _local9, _arg1.isoName);
                _local3[1] = _local10;
                _local10 = this.getComboRim(_local6, _local7, (_local9 + 1), (_local9 + 1), _arg1.isoName);
                _local3[2] = _local10;
                _local10 = this.getComboRim((_local6 - 1), (_local6 - 1), _local8, _local9, _arg1.isoName);
                _local3[3] = _local10;
            };
            return (_local3);
        }
        private function getComboRim(_arg1:int, _arg2:int, _arg3:int, _arg4:int, _arg5:String):IsoObject{
            var _local6:int;
            var _local7:int;
            var _local8:IsoObject;
            var _local9:IsoMapItem;
            var _local10:Array;
            var _local11:Boolean;
            var _local12:int;
            var _local13:IsoObject;
            _local6 = _arg1;
            while (_local6 <= _arg2) {
                _local7 = _arg3;
                while (_local7 <= _arg4) {
                    _local9 = this._map.getMapItem(_local6, _local7);
                    if (_local9 == null){
                        return (null);
                    };
                    _local10 = _local9.isoList;
                    _local11 = false;
                    _local12 = 0;
                    while (_local12 < _local10.length) {
                        _local13 = (_local10[_local12] as IsoObject);
                        if (_local13.isoName == _arg5){
                            if ((((_local8 == _local13)) || ((_local8 == null)))){
                                _local11 = true;
                                _local8 = _local13;
                                break;
                            };
                        };
                        _local12++;
                    };
                    if (!_local11){
                        return (null);
                    };
                    _local7++;
                };
                _local6++;
            };
            return (_local8);
        }
        public function toOutPoint(_arg1:Point):Point{
            var _local2:Number = (_arg1.x + (this.x - this.displayRect.x));
            var _local3:Number = (_arg1.y + (this.y - this.displayRect.y));
            return (new Point(_local2, _local3));
        }
        public function toInnerPoint(_arg1:Point):Point{
            var _local2:Number = (_arg1.x - (this.x - this.displayRect.x));
            var _local3:Number = (_arg1.y - (this.y - this.displayRect.y));
            return (new Point(_local2, _local3));
        }
        public function tryPut(_arg1:IsoPosition, _arg2:IsoPosition, _arg3:IsoObject):IsoTryResult{
            var _local10:int;
            var _local11:int;
            var _local12:IsoMapItem;
            var _local13:IsoObject;
            var _local4:IsoTryResult = new IsoTryResult();
            var _local5:IsoObject;
            var _local6:Boolean;
            var _local7:Number = 0;
            var _local8:int = _arg1.x;
            var _local9:int = _arg2.x;
            while (_local8 < _local9) {
                _local10 = _arg1.z;
                _local11 = _arg2.z;
                while (_local10 < _local11) {
                    _local12 = this._map.getMapItem(_local8, _local10);
                    if (_local12 == null){
                        return (_local4);
                    };
                    _local13 = _local12.topIsoObject;
                    if (((!((_local13 == null))) && (((_local13.y - _local13.altitude) < _local7)))){
                        _local7 = (_local13.y - _local13.altitude);
                    };
                    if (((_local6) && (!((_local13 == _local5))))){
                        _local4.y = _local7;
                        return (_local4);
                    };
                    _local5 = _local13;
                    _local6 = true;
                    _local10++;
                };
                _local8++;
            };
            if (_local5 == null){
                _local4.success = true;
                _local4.y = 0;
                _local4.position = new IsoPosition(_arg1.x, _arg1.z);
            } else {
                if (_local5.admissible(_arg3)){
                    _local4.success = true;
                    _local4.y = (_local5.y - _local5.altitude);
                    _local4.position = new IsoPosition(_arg1.x, _arg1.z);
                } else {
                    _local4.success = false;
                    _local4.y = _local7;
                    _local4.position = new IsoPosition(_arg1.x, _arg1.z);
                };
            };
            return (_local4);
        }

    }
}//package com.fminutes.vinciIsometric.core 

import flash.geom.*;

class VirtualWall {

    public var xyz:Vector3D;
    public var floorHeight:Number;
    public var floorWidth:Number;
    public var direction:int;
    public var x:Number;
    public var y:Number;
    public var z:Number;
    public var altitude:Number;

    public function VirtualWall(){
    }
}
